/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.item.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.item.entity.ItemEntity;
import org.springblade.modules.item.service.IItemService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.item.entity.ItemImgEntity;
import org.springblade.modules.item.vo.ItemImgVO;
import org.springblade.modules.item.service.IItemImgService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 * 教具图片 控制器
 *
 * @author
 * @since 2023-03-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-itemImg/itemImg")
@Api(value = "教具图片", tags = "教具图片接口")
public class ItemImgController extends BladeController {

	private final IItemImgService itemImgService;
	private final IItemService itemService;

	/**
	 * 教具图片 详情
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入itemImg")
	public R<ItemImgVO> detail(ItemImgEntity itemImg) {
		ItemImgEntity detail = itemImgService.getOne(Condition.getQueryWrapper(itemImg));
		ItemEntity item = itemService.getById(detail.getItemId());
		ItemImgVO vo=new ItemImgVO();
		BeanUtil.copyProperties(detail,vo);
		if (null!=item){
			vo.setItemName(item.getItemName());
		}
		return R.data(vo);
	}

	/**
	 * 教具图片 自定义分页
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "自定义分页", notes = "传入itemImg")
	public R<IPage<ItemImgVO>> page(ItemImgVO itemImg, Query query) {
		IPage<ItemImgVO> pages = itemImgService.selectItemImgPage(Condition.getPage(query), itemImg);
		return R.data(pages);
	}

	/**
	 * 教具图片 新增
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入itemImg")
	public R save(@Valid @RequestBody ItemImgEntity itemImg) {
		if (null!=itemImg.getIsMain()&&itemImg.getIsMain()==1){
			itemImgService.updateIsMain(itemImg.getId(),itemImg.getItemId(),itemImg.getIsMain());
		}
		return R.status(itemImgService.save(itemImg));
	}

	/**
	 * 教具图片 修改
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入itemImg")
	public R update(@Valid @RequestBody ItemImgEntity itemImg) {
		if (itemImg.getIsMain()==1){
			itemImgService.updateIsMain(itemImg.getId(),itemImg.getItemId(),itemImg.getIsMain());
		}
		return R.status(itemImgService.updateById(itemImg));
	}


	/**
	 * 教具图片 删除
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/remove")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(itemImgService.deleteLogic(Func.toLongList(ids)));
	}


}
